﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using WinForms=System.Windows.Forms;

namespace Coding4Fun.PrinterDisplayHack
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
		private System.Windows.Forms.NotifyIcon _notifyIcon;
        private MainWindow _mainWindow;

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            // stop the timer
            _mainWindow.StopPrinterHacking();

            // display the error
            _mainWindow.LogText("Sending the stock prince to the printer was halted due to this error:" + e.Exception.ToString());

            // display the form
            ShowMainForm();

            // Log the error to a file and notify the user
            Exception theException = e.Exception;
            string theErrorPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\PrinterDisplayHackError.txt";
            using (System.IO.TextWriter theTextWriter = new System.IO.StreamWriter(theErrorPath, true))
            {
                DateTime theNow = DateTime.Now;
                theTextWriter.WriteLine(String.Format("The error time: {0} {1}", theNow.ToShortDateString(), theNow.ToShortTimeString()));
                while (theException != null)
                {
                    theTextWriter.WriteLine("Exception: " + theException.ToString());
                    theException = theException.InnerException;
                }
            }
            MessageBox.Show("An unexpected error occurred.  A stack trace can be found at:\n" + theErrorPath);
            e.Handled = true;
        }

        private void ShowMainForm()
        {
            // show the window in the normal state
            _mainWindow.Show();
            _mainWindow.WindowState = WindowState.Normal;
        }
        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            ShowMainForm();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            _notifyIcon = new WinForms.NotifyIcon();
            _notifyIcon.DoubleClick += notifyIcon_DoubleClick;

            _notifyIcon.Icon = PrinterDisplayHack.Properties.Resources.Icon;
            _notifyIcon.Visible = true;

            WinForms.MenuItem[] items = new[]
            {
                new WinForms.MenuItem("&Settings", Settings_Click) { DefaultItem = true } ,
                new WinForms.MenuItem("-"),
                new WinForms.MenuItem("&Exit", Exit_Click)
            };

            _notifyIcon.ContextMenu = new WinForms.ContextMenu(items);

            _mainWindow = new MainWindow();

            if (!_mainWindow.SettingsAreValid())
                _mainWindow.Show();
            else
                _mainWindow.StartPrinterHacking();
        }

        private void Settings_Click(object sender, EventArgs e)
        {
            _mainWindow.Show();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            _notifyIcon.Dispose();
        }
    }
}
